<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Carbarn extends Model
{
    protected $table = 'mckj_carbarn';

    protected $fillable = [
        'name', 'code', 'addTime', 'address', 'shortAddr',
        'longitude', 'latitude', 'mcstationId', 'boxId',
        'coverPath', 'navigateImg', 'tipInfo', 'softTips',
        'provinceCode', 'cityCode', 'districtCode',
    ];

    const CREATED_AT = 'addTime';

    const UPDATED_AT = null;

    protected $dateFormat = 'U';

    public function station()
    {
        return $this->belongsTo(Station::class, "mcstationId");
    }

    public function runStates()
    {
        return $this->hasMany(RunState::class, "carbarnId");
    }

    public static function listBoxOptions()
    {
        $result = [];
        Station::with('carbarns')->get()->map(function ($station) use (&$result) {
            $station->carbarns->map(function ($carbarn) use (&$result, $station) {
                $result[$carbarn->id] = sprintf('%s——%s', $station->name, $carbarn->name);
            });
        });
        return $result;
    }
}
